import { AxiosError } from 'axios';
import { createVNode } from 'vue';
import { Modal } from 'ant-design-vue';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { useUserStore } from '@/store';

let instance = null;
export function handleLoginExpire(_, res) {
  const userStore = useUserStore();

  if (instance === null) {
    instance = Modal.confirm({
      title: '提示',
      icon: createVNode(ExclamationCircleOutlined),
      content: res.data.message,
      onOk: () => {
        userStore.logout();
        instance = null;
      },
      onCancel: () => {
        instance = null;
      },
    });
  }

  const code = 'ERR_LOGIN_EXPIRE';
  const message = 'Login authorization invalidated.';
  const error = new AxiosError(message, code, res.config);

  return Promise.reject(error);
}
